<?php

namespace App\Admin\Models;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Admin\Common as AdminCommon;

class SiteStoreContract extends Model
{
    protected $table = 'site_store_contract_list';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];



    /**
     * 获取列表
     * 添加门店时, 合同列表数据 限制为 试用期数据 20天，合同列表不展示数据
     * 正式合同需要添加后，才能在列表展示 审核
     *
     */
    public static function getList(Request $request)
    {
        $query = self::query()
            ->with(['companyinfo','adminuserinfo','businessmeminfo','paytypeinfo','sitestore','customerserviceinfo'])
            ->where('is_del',1);

        $isgetall = intval($request->isgetall) ?? 0;

        $company_name = $request->company_name;
        if ($company_name != '') {
            $cp_ids=SiteCompany::where('company_name', 'like', '%' . $company_name . '%')->pluck('id');
            $query->whereIn('cp_id', $cp_ids);
        }

        $contact_phone = $request->contact_phone;
        if ($contact_phone != '') {
            $cp_ids=SiteCompany::where('contact_phone', $contact_phone)->pluck('id');
            $query->whereIn('cp_id', $cp_ids);
        }
        $contact_name = $request->contact_name;
        if ($contact_name != '') {
            $cp_ids=SiteCompany::where('contact_name', $contact_name)->pluck('id');
            $query->whereIn('cp_id', $cp_ids);
        }
        $query->orderBy('id', 'desc');
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function companyinfo(){
        return $this->hasOne(SiteCompany::class, 'id', 'cp_id');
    }

    public function adminuserinfo(){
        return $this->hasOne(AdminUser::class, 'id', 'admin_user_id');
    }

    public function brandinfo(){
        return $this->hasOne(Brand::class, 'id', 'brand_id');
    }

    public function businessmeminfo(){
        return $this->hasOne(BusinessMem::class, 'id', 'business_mem_id');
    }

    public function customerserviceinfo(){
        return $this->hasOne(CustomerServiceMem::class, 'id', 'customer_service_id');
    }

    public function paytypeinfo(){
        return $this->hasOne(AdminPaytypeList::class, 'id', 'pay_type');
    }

    public function sitestore(){
        return $this->belongsTo(SiteStore::class,'store_relation_id','id');
    }
}
